﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Ninject;
using SydEnergiPrototype.DataAccess;
using SydEnergiPrototype.Logic;
using SydEnergiPrototype.Model;

namespace SydEnergiPrototype.ViewModel
{
    public class DataViewModel : ViewModelBase, IDataViewModel
    {
        private RelayCommand _clearFieldsCommand;
        public RelayCommand ClearFieldsCommand
        {
            get 
            { 
                if(_clearFieldsCommand == null)
                    _clearFieldsCommand = new RelayCommand(param => Clear());

                return _clearFieldsCommand;
            }
        }

        private List<SolidColorBrush> _colorCodes;
        public List<SolidColorBrush> ColorCodes
        {
            get
            {
                if (_colorCodes == null)
                    _colorCodes = _colorCodeDb.GetColorCodes();

                return _colorCodes;
            }
            set { _colorCodes = value; Notify("ColorCodes"); }
        } 
        private IEntry _currentEntry;
        public IEntry CurrentEntry
        {
            get { return _currentEntry; }
            set { _currentEntry = value; Notify("CurrentEntry"); }
        }

        private RelayCommand _saveEntry;
        public RelayCommand SaveEntryCommand
        {
            get
            {
                if(_saveEntry == null)
                    _saveEntry = new RelayCommand(param => Save());

                return _saveEntry;
            }
        }

        public string Header
        {
            get { return "DataView"; }
        }

        private List<string> _cableOptions; 
        public List<string> CableOptions
        {
            get
            {
                if(_cableOptions == null)
                {
                    _cableOptions = new List<string>{"Blæsefiber 2 SM, Emtelle", "Blæsefiber 8 SM, Emtelle"};
                }
                return _cableOptions;
            }
        }

        private List<string> _teletubeOptions; 
        public List<string> TeletubeOptions
        {
            get
            {
                if(_teletubeOptions == null)
                {
                    _teletubeOptions = new List<string>{"7x2mm100 7x2 A", "7x5mm102 7x5 B"};
                }
                return _teletubeOptions;
            }
        }

        private int _cableSelectedIndex;
        public int CableSelectedIndex
        {
            get
            {
                if (!_isInitialized && _currentEntry != null)
                    InitializeSelectedIndexes();

                return _cableSelectedIndex;
            }
            set 
            {
                _cableSelectedIndex = (value >= 0 ? value : 0); 
                Notify("CableSelectedIndex");
                CurrentEntry.Cable = CableOptions[_cableSelectedIndex];
            }
        }

        private int _teletubeSelectedIndex;
        public int TeletubeSelectedIndex
        {
            get
            {
                if (!_isInitialized && _currentEntry != null)
                    InitializeSelectedIndexes();

                return _teletubeSelectedIndex;
            }
            set
            {
                _teletubeSelectedIndex = (value >= 0 ? value : 0);
                Notify("TeletubeSelectedIndex");
                CurrentEntry.Teletube = TeletubeOptions[_teletubeSelectedIndex];
            }
        }

        private int _fiberSelectedIndex;
        public int FiberSelectedIndex
        {
            get
            {
                if(!_isInitialized && _currentEntry != null)
                    InitializeSelectedIndexes();

                return _fiberSelectedIndex;
            }
            set 
            {
                _fiberSelectedIndex = (value >= 0 ? value : 0); 
                Notify("FiberSelectedIndex");
                CurrentEntry.Fiber = ColorCodes[_fiberSelectedIndex];
            }
        }

        private int _tubeSelectedIndex;
        public int TubeSelectedIndex
        {
            get
            {
                if (!_isInitialized && _currentEntry != null)
                    InitializeSelectedIndexes();

                return _tubeSelectedIndex;
            }
            set 
            {
                _tubeSelectedIndex = (value >= 0 ? value : 0); 
                Notify("TubeSelectedIndex");
                CurrentEntry.Tube = ColorCodes[_tubeSelectedIndex];
            }
        }

        private readonly IFactory _factory;
        private readonly IColorCodeDB _colorCodeDb;
        private bool _isInitialized;

        [Inject]
        public DataViewModel(IColorCodeDB colorCodeDb, IFactory factory)
        {
            if(colorCodeDb == null)
                throw new ArgumentNullException("colorCodeDb");
            _colorCodeDb = colorCodeDb;

            if(factory == null)
                throw new ArgumentNullException("factory");
            _factory = factory;

            _isInitialized = false;
        }

        private void InitializeSelectedIndexes()
        {
            CableSelectedIndex = CableOptions.IndexOf(CurrentEntry.Cable);
            TeletubeSelectedIndex = TeletubeOptions.IndexOf(CurrentEntry.Teletube);
            FiberSelectedIndex = ColorCodes.IndexOf(ColorCodes.Single(c => c.Color == CurrentEntry.Fiber.Color));
            TubeSelectedIndex = ColorCodes.IndexOf(ColorCodes.Single(c => c.Color == CurrentEntry.Tube.Color));
            _isInitialized = true;
        }

        private void Save()
        {}

        private void Clear()
        {
            _currentEntry = _factory.CreateEntry();
        }
    }
}
